<?php

namespace app\admin\controller;

use app\admin\model\HorkeyModel;
use app\admin\validate\HorkeyValidate;
use app\admin\validate\CommonnValidate;
use app\BaseController;
use think\facade\Request as faRequest;

class Horkey extends BaseController
{
    protected $middleware = ['Auth'];

    public function list() {
        $params = faRequest::post(['pageNo', 'pageSize', 'type']);
        $validate = new CommonnValidate();
        if (!$validate->scene('list')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new HorkeyModel();
        $list = $sql
            ->order('sort desc')
            ->order('create_at desc')
            ->paginate([
                'list_rows'=> request()->param('pageSize'),
                'page' => request()->param('pageNo'),
            ]);
        return result()->success($list);
    }

    public function addEdit() {
        $params = faRequest::post(['id']);
        if(isset($params['id']) && $params['id']) {
            return $this->add();
        }
        return $this->edit();
    }

    public function add() {
        $params = faRequest::post(['horName', 'sort']);
        $validate = new HorkeyValidate();
        if (!$validate->scene('add')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new HorkeyModel();
        $sql->save($params);
        return result()->success("添加成功");
    }

    public function edit() {
        $params = faRequest::post(['id', 'horName', 'sort']);
        $validate = new CommonnValidate();
        if (!$validate->scene('edit')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new HorkeyModel();
        $info = $sql->find($params['id']);
        $info->save($params);
        return result()->success("修改成功");
    }

    public function dels() {
        $params = faRequest::post(['ids']);
        $validate = new CommonnValidate();
        if (!$validate->scene('dels')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new HorkeyModel();
        $sql->destroy($params['ids']);
        return result()->success("删除成功");
    }
}